%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A) Stata code and data to obtain Figures 3 and 4; Tables 3 - 12 in the paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1. Set up the folder location:
1.1 Make sure that the current folder contains a "Pseudodata" sub-folder with the data input. 
1.3 Make sure that the current folder contains an empty "output" sub-folder where the figures (in .pdf format) and tables (in .tex format) will be stored.
2. The programs are run in Stata. Make sure that the correct Stata packages at the top of "TDS_Figures_Mainpaper.do" and "TDS_Tables_Mainpaper.do" are installed

3. Run the "TDS_Figures_Mainpaper.do" code to produce Figure 3 and 4 in the paper.  
3.1 The code extracts data from the "Pseudodata" folder.

4. Run the "TDS_Tables_Mainpaper.do" code to produce Tables 3 - 12 in the paper.
4.1 The code extracts data from the "Pseudodata" folder.
5. Upon running the code, the results will appear as .pdf or .tex files in the "output" folder


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Description for processing the .dat datasets contained in the "pseudo_data" folder
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1. Data sources and instructions to access licensed data
1.1 Loan Pricing Corporation (LPC) DealScan database. This data is available upon subscription, and was obtained through WRDS. We use this data to obtain loan information including covenant thresholds, loan amount, maturity for contructing our strictness measure and regressions with loan level controls. Processed data from this source are used in all figures and tables with strictness measure of loan-level controls in the paper. 
1.2 Compustat. This data is available upon subscription, and was obtained through WRDS. We use this data to compute underlying financial ratios of covenants and firms level charateristics, such as book-to-market ratio, profitabiltiy, SA index, WW index and so on. Underlying financial ratios are main input we use to construct our strictness measure. Processed data from this source are used in all figures and tables. 
1.3 CRSP. This data is available upon subscription, and was obtained through WRDS. We use this data to obtain monthly stock returns, to compute market cap over time. Processed data from this source are used in Figure 4 and Tables 3, 4, 5, 6, 7, 8, 9, 10.
1.4 Compustat bank.  This data is available upon subscription, and was obtained through WRDS. We use this data to compute bank-level controls (Tier 1 ratio and leverage) used in Table 12. 
1.5 NIPA data. This data is publicly available. Nominal quantities and corresponding price indexes come from Tables 1.1.5 and 1.1.4 of the National Income and Products Accounts (NIPA) tables from the U.S. Bureau of Economic Analysis (BEA). We use this data to construct quarterly real GDP and PCE grwoth rate.  Processed data from this source are used in Table 9 to 12.
1.6 Federal Reserve Bank of St. Louis. This data is available free of charge. We the the quarterly GDPC1 series for ploting in our Figure 3. 
1.7 Fama-French 5 factors. This data is publicly available on the website of Kenneth French https://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html
1.8 q factors and expected growth portfolio returns. This data is publicly available on https://global-q.org/factors.html
1.9 DealScan-Compustat linking table in Chava and Robert 2008. This data is publicly available from the website of Michael Roberts https://finance.wharton.upenn.edu/~mrrobert/research.html 
1.10 DealScan-Lender linking table in Schwert 2018.  This data is publicly available from the website of Michael Schwert https://sites.google.com/site/mwschwert/data-and-code?authuser=0


2. Intermediate data processing and merging
2.1 Download the universe of the original data from the sources in point 1. 
2.2 Merge the datasets:
2.2.1 Merge DealScan data with Compustat using the linking table in Chava and Robert 2008.
2.2.2 Merge Compustat with CRSP using the standard approach. 
2.2.3 DealScan and Compustat bank are merged using DealScan lender linking table in Schwert 2018.


3. Resulting output in the "pseudo_data" folder. These datasets are pseudo datasets based on the originals, where all variables are randomly reshuffled and bank identifiers are masked
3.1 "ccm41a.dta": is the final dataset, which contains firm-level monthly stock returns from CRSP, accouting variables from Compustat. 
3.2 "5port_firm_characteristics.dta" contains the portfolio-level charateristics listed in Table 5. 
3.3 "comp_ratios.dta" contains firm-level underlying finanical ratios of 5 types of covenant (used to contruct strictness measure). We compute those financial ratios using Compustat dataset following the definitions in Demerjian and Owens, 2016.
3.4 "Csave_5ff_whole.dta" contains monthly portfolio level returns. Portfolios are sorted on strictness measure, using "ccm41a.dta".
3.5 "deallink.dta" contains loan-level information (loan amount, maturity, covenants, lead bank of loan sydication) from DealScan, merged with the linking table in Chava and Robert 2008. 
3.6 "EGportfolios.dta" contains quartlery returns of 6 portfolios double sorted on Size and Expected growth. The data is availabe on the website of https://global-q.org/factors.html. The version we downloaded "benportf_me_eg_quarterly_2021.csv". 
3.7 "GDPgrowth.dta" is the quarterly GDP growth data downloaded from the website of Federal Reserve Bank of St. Louis. The data series is GDPC1. 
3.8 "hxz_egfactor.dta" contains monthly returns of q5 facgtors, it is downloaded from https://global-q.org/factors.html
3.9 "macro_indicator.dta" contains quarterly GDP and PCE growth rate constructed using Tables 1.1.5 and 1.1.4 of the National Income and Products Accounts (NIPA) from the U.S. Bureau of Economic Analysis (BEA). 
3.10 "package_lenders.dta" contains quarterly lender (lead bank of loan package in DealScan) level controls (Tier 1 ratio and leverage). 
3.11 "Price_Risk.dta" contains quarterly returns of strictness sorted portfolios, quarterly returns are computed as componded monthly returns. 
3.12 "rdd_data.dta" contains stock returns (from CRSP), covenant threshold (from Dealscan) and accouting variables (from Compustat) for RDD analysis in Table 8. 
3.13 "strictness.dta" contains our quarterly strictness measure. 




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% B) Matlab code and data to obtain Figure 1 and 2, and Table 1, 2 and panel B of Table 3 in the paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1. Set up the folder location:
1.1 Make sure that the current folder contains a "Model with Risk Shocks" sub-folder and a "Model without Risk Shocks" subfolder with dynare codes. 
1.2 Make sure that the current folder contains an empty "output" sub-folder where the figures (in .eps format) will be stored.

2.0 Install dynare. Go to https://www.dynare.org/, download the package and install it on your computer. We are using Dynare 4.5.3 for our model computation.
2.1 Run the "Tables12.m" code
2.2 The code first produces parameters listed in Table 1.
2.3 Then, it runs the dynare code of the model with and without covenants in the case with risk shocks. 
2.4 Finally, the code uses the simulated data generated from Dynare to compute simulated moments in Table 2.

3. Run the "Figure1.m" code (you need to run Tables12.m first) 
3.1 Once you run the dynare code, you can run "Figure1.m" code to produce impulse response functions in Figure 1. 
3.2 Upon running the code, the results will appear as .eps in the "output" folder

4. Run the "Figure2.m" code to produce impulse response functions in Figure 2. 

5. Run the "Table3.m" code (you need to run Tables12.m first)
5.1 Once you run the dynare code in "Tables12.m", then you can run "Table3.m" code to produce panel B in Table 3. 















